/*
 * RectangularBoatShape.cpp
 *
 *  Created on: Jun 21, 2009
 *      Author: computer
 */

#include "RectangularBoatShape.h"

RectangularBoatShape :: RectangularBoatShape(const string& _name, const Point<int>& _origin, const Dimension<int>& _size, char _fillChar = 'n')
: Boat(_name, _origin), size(_size), fillChar(_fillChar) {
	hitPoints = new bool*[size.getHeigth()];
	for (int i = 0; i < size.getHeigth(); ++i) {
		hitPoints[i] = new bool[size.getWidth()];
		for (int j = 0; j < size.getWidth(); ++j) {
			hitPoints[i][j] = false;
		}
	}
}

RectangularBoatShape :: ~RectangularBoatShape() {
	for (unsigned int i = 0; i < size.getHeigth(); ++i) delete[] hitPoints[i];
	delete[] hitPoints;
}

bool RectangularBoatShape :: fireToBoat(const Point<int>& pt, Arena* ar) {
	int _x = pt.getX() - getOrigin().getX(), _y = pt.getY() - getOrigin().getY();
	if (_x >= 0 && _y >= 0 && _x < size.getWidth() && _y < size.getHeigth()) {
		hitPoints[_y][_x] = true;
		ar->setFiredPoint(pt);
		return true;
	}
	else return false;
}

void RectangularBoatShape :: write(Arena* ar) throw (OutOfBound, OverlapBoat, TooNearBoat) {
	for (unsigned int i = 0; i < size.getWidth(); ++i) {
		for (unsigned int j = 0; j < size.getHeigth(); ++j) {
			ar->setPoint(Point<int>(i,j) + getOrigin());
		}
	}
}

void RectangularBoatShape :: writeDestroyed(Arena* ar) {
	for (unsigned int i = 0; i < size.getWidth(); ++i) {
		for (unsigned int j = 0; j < size.getHeigth(); ++j) {
			ar->setDestroyedPoint(Point<int>(i,j) + getOrigin());
		}
	}
}

bool RectangularBoatShape :: isDestroyed() {
	for (unsigned int i = 0; i < size.getHeigth(); ++i) {
		for (unsigned int j = 0; j < size.getWidth(); ++j) {
			if (!hitPoints[i][j]) {
				return false;
			}
		}
	}
	return true;
}

unsigned int RectangularBoatShape :: getWidth() {
	return size.getWidth();
}

void RectangularBoatShape :: setWidth(unsigned int _width) {
	size.setWidth(_width);
	for (unsigned int i = 0; i < size.getHeigth(); ++i) {
		hitPoints[i] = new bool[size.getWidth()];
		for (unsigned int j = 0; j < size.getWidth(); ++j) {
			hitPoints[i][j] = false;
		}
	}
}

unsigned int RectangularBoatShape :: getHeigth() {
	return size.getHeigth();
}

void RectangularBoatShape :: setHeigth(unsigned int _heigth) {
	size.setHeigth(_heigth);
	for (int i = 0; i < size.getHeigth(); ++i) {
		hitPoints[i] = new bool[size.getWidth()];
		for (int j = 0; j < size.getWidth(); ++j) {
			hitPoints[i][j] = false;
		}
	}
}

